package com.duoduo.admin.controller;

import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.mason.duoduo.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.duoduo.admin.api.dto.SysRoleDTO;
import com.duoduo.admin.api.vo.RoleExcelVO;
import com.duoduo.admin.api.vo.RoleVO;
import com.duoduo.admin.service.SysRoleMenuService;
import com.duoduo.admin.service.SysRoleService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
@Tag(name = "角色管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysRoleController {
	
	private final SysRoleService sysRoleService;
	
	private final SysRoleMenuService sysRoleMenuService;
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	public R<Page<SysRoleDTO>> page(@RequestBody PageRequest<SysRoleDTO> request){
		Page<SysRoleDTO> page = sysRoleService.pageMenus(request.getPage(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	public R<SysRoleDTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(sysRoleService.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public R<SysRoleDTO> create(@RequestBody SysRoleDTO request){
		sysRoleService.createRole(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("update")
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public R<SysRoleDTO> update(@RequestBody SysRoleDTO request){
		sysRoleService.updateRole(request);
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public R<SysRoleDTO> delete(@Parameter(description = "主键id") @PathVariable("id")Long id){
		sysRoleService.delete(id);
		return R.ok();
	}
	
	/**
	 * 获取角色列表
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R<List<SysRoleDTO>> listRoles() {
		return R.ok(sysRoleService.listRoles());
	}
	/**
	 * 更新角色菜单
	 * @param roleVO 角色对象
	 * @return success、false
	 */
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R<Void> saveRoleMenus(@RequestBody RoleVO roleVO) {
		sysRoleMenuService.saveRoleMenus(roleVO.getRoleId(), roleVO.getMenuIds());
		return R.ok();
	}

	/**
	 * 导出excel 表格
	 * @return
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('sys_role_import_export')")
	public List<RoleExcelVO> export() {
		return sysRoleService.listRole();
	}

	/**
	 * 导入角色
	 * @param excelVOList 角色列表
	 * @param bindingResult 错误信息列表
	 * @return ok fail
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('sys_role_import_export')")
	public R importRole(@RequestExcel List<RoleExcelVO> excelVOList, BindingResult bindingResult) {
		return sysRoleService.importRole(excelVOList, bindingResult);
	}
}
